/**
 * Created by root on 2015/1/17.
 */
var doPage = function(page, limit){
    $.post('/admin/user', {
        page: page,
        limit: limit
    }, function(res){
        var result = res.result;
        var count = res.count;
        var pageCount = res.pageCount;
        var currentPage = res.currentPage;
        var limit = res.limit;

        var oPageUl = $('#js-page');
        var oStart = $('#js-start')
        var oEnd = $('#js-end');
        var oTotal = $('#js-total');
        var oUserBody = $('#js-user-body');

        oStart.text((currentPage - 1) * limit + 1);
        oEnd.text((currentPage * limit) > count ? count : (currentPage * limit));
        oTotal.text(count);

        var html = '';
        for(var i= 0,len=result.length; i < len;i++){
            var user = result[i];
            html += '<tr>';
            html +=	'<td><p class="text-center tb-text">' + user.sid + '</p></td>';
            html += '<td><p class="text-left tb-text"><input type="text" data-sid="' + user.sid + '" data-origin="' + user.name + '" name="username" value="' + user.name + '"/></p></td>';
            html += '<td><p class="text-left tb-text"><input type="text" data-sid="' + user.sid + '" data-origin="' + user.profession + '" name="profession" value="' + user.profession + '"/></p></td>';
            //html +=	'<td><p class="text-center tb-text">' + user.profession + '</p></td>';
            html +=	'<td><p class="text-center tb-text">' + user.score + '</p></td>';
            html +=	'<td><p class="text-center tb-text">' + user.battles + '</p></td>';
            html +=	'<td><p class="text-center tb-text">' + user.success + '</p></td>';
            html +=	'<td><p class="text-center tb-text">' + user.failure + '</p></td>';
            html +=	'<td><p class="text-center tb-text">' + user.flee + '</p></td>';
            html += '<td><p class="text-center tb-text"><a class="btn mini black" href="/admin/report/battle?sid=' + user._id + '"><i></i> 导出</a></p></td>';
            html += '</tr>';
        }
        oUserBody.html(html);

        var page = '';
        if(currentPage > 1){
            page += '<li class="prev"><a data-page="' + (currentPage - 1) + '">← 上一页</a></li>';
            if(currentPage > 4){
                page += '<li><a data-page="1">' + 1 + '</a></li>';
            }
            if(currentPage > 5){
                page += '<li class="disabled"><a> ... </a></li>';
            }
        }

        for(var i=3; i > 0; i--){
            if((currentPage - i) > 0){
                page += '<li class=""><a data-page="' + (currentPage - i) + '">' + (currentPage - i) + '</a></li>';
            }
        }

        for(var i=currentPage;i<=pageCount;i++){
            if(i == (currentPage + 4)){
                break;
            }
            if(i == currentPage){
                page += '<li class="active"><a>' + i + '</a></li>';
            } else {
                page += '<li class=""><a  data-page="' + i + '">' + i + '</a></li>';
            }
        }

        if(pageCount != currentPage){

            if(currentPage < (pageCount - 4)){
                page += '<li class="disabled"><a> ... </a></li>';
            }

            if(currentPage < (pageCount - 3)){
                page += '<li><a data-page="' + pageCount + '">' + pageCount + '</a></li>';
            }
            page += '<li class="next"><a  data-page="' + (currentPage + 1) + '">下一页 → </a></li>';
        }
        oPageUl.html(page);

        var oPagesLi = $('#js-page li');
        oPagesLi.click(function () {
            if($(this).hasClass('disabled') || $(this).hasClass('active')){
                return;
            }
            var page = $(this).find('a').data('page');
            doPage(page, 10);
        });
    }, 'json');
}

$(function(){
    doPage(1, 10);
});

/**
 * var oUserBody = $('#js-user-body');
 */

var oUserBody = $('#js-user-body');
var oNameInput = oUserBody.find('input[name="username"]');
var oProfessionInput = oUserBody.find('input[name="profession"]');

oUserBody.delegate('input[name="username"]', 'blur', function () {
    var value = $(this).val();
    var origin = $(this).data('origin');
    if(value != origin){
        $.post('/admin/updateUser', {
            name: value,
            sid: $(this).data('sid')
        }, function(res){
            var d = dialog({
                content: '姓名修改成功'
            });
            d.showModal();
            setTimeout(function () {
                d.close().remove();
            }, 1000);
        });
    }
})

oUserBody.delegate('input[name="profession"]', 'blur', function () {
    var value = $(this).val();
    var origin = $(this).data('origin');
    if(value != origin){
        $.post('/admin/updateUser', {
            profession: value,
            sid: $(this).data('sid')
        }, function(res){
            var d = dialog({
                content: '岗位修改成功'
            });
            d.showModal();
            setTimeout(function () {
                d.close().remove();
                window.location.reload();
            }, 1000);
        });
    }
})

$('#js-upload-user').submit(function(){
    if(!$('#fileInput').val()){
        return false;
    }
});